package com.drawm.adt.lagControl {
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.display.DisplayObjectContainer;
	import flash.utils.getTimer;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	/**
	 * @author Drawm
	 */
	public class LagControl extends Sprite{
//		System speed = frameRate
		private static var _siSpeed : int = 0;
		
		private static var _tc : int;
		private static var _idealSpeed : Number;
		private static var _tcav : int;
		private static var _FPS : Number;
		private static var _timer : Timer;
		private static var _frame : Number;
		private static var _lastTime : int = 0;
		private static var _markTime : int = 0;
		private static var _movieFrameRate : Number;
		private static var _traceTime : Number;
		private static var _siFPS : Number;
		private static var _exeFct : Function;
		private static var isUIEnable:Boolean = false;
		
		private var _textField:TextField;
		private var _fixField:TextField;
		private var _width:int = 80;
		private var _height:int = 200;
		private var _backgroundColor:int = 0xEEEEEE;
		private var _version:Number = 0.1;
		private static var _status : String = "IDLE";

		//		NON STATIC FUNCTION ---------------------------------------------------------------------//
		public function LagControl(){
			if(isUIEnable){
				
				_fixField = new TextField();
				_fixField.width = _width;
				_fixField.height = _height;
				_fixField.backgroundColor = _backgroundColor;
	//			Add more text option later
				addChild(_fixField);
				_fixField.text = "LagControl v "+_version+"\nStatus = \nFPS = \nCurrentSpeed = \nIdealSpeed = ";
				
				_textField = new TextField();
				_textField.width = 80;
				_textField.height = _height;
				_textField.x = 80;//_fixField.x+_fixField.width-_textField.width;
				_textField.backgroundColor = _backgroundColor;
	//			Add more text option later
				addChild(_textField);
			}
		}

		private function resetUi() : void {
			_textField.text = "\n"+_status+"\n"+_FPS+"\n"+_tcav+"\n"+_idealSpeed;
		}
//		NON STATIC FUNCTION END -----------------------------------------------------------------//

//		STATIC FUNCTION START -------------------------------------------------------------------//

//		Init the app
		public static function init(fct:Function, container:DisplayObjectContainer = null):void
		{
			//_db._efClass._state = 1;
//			If there is no _siSpeed, start teh app
			if (_siSpeed == 0){
				
//				If there is a container, add it to the stage
				if(container != null){
					isUIEnable = true;
					var lagControl:LagControl = new LagControl();
					container.addChild(lagControl);
//					Make shure the ui will be updated
					fct = new function():void{
						lagControl.resetUi();
						fct.call();
					};
				}
				
				_status = "WORKING";
				
//				Reset needed value before we start
				_tc = 1;
				_tcav = 0;
				_frame = 0;
				_markTime = getTimer();
				_idealSpeed = 1000/_FPS;
				_siSpeed = int(1000/_FPS);
				
			
//				Make a timer to emulate an EnterFrame event
				_timer = new Timer(_siSpeed);
				_exeFct = fct;
				_timer.addEventListener(TimerEvent.TIMER, sitime);
				_timer.start();
			}
		}

//		Execute function
		private static function sitime(e:TimerEvent):void
		{
			++_frame;
			_exeFct.call();
			tick();
		}

//		Check if the speed should be increased or decreased
		private static function tick():void
		{
			_lastTime = _markTime;
			_markTime = getTimer();
			_tcav = _markTime-_lastTime;

			_traceTime = _tcav/_movieFrameRate;
			_siFPS = (_idealSpeed/_traceTime)*_FPS;

			if (_siSpeed < 1) 
				_siSpeed = 1;

			_siSpeed = int(((_idealSpeed*_siSpeed)/_traceTime)/_movieFrameRate);
			_timer.delay=_siSpeed;
			_timer.reset();
			_timer.start();
		}
//		STATIC FUNCTION END ------------------------------------------------------------------//
	}
}
